package com.cn.service.server.TechnologicalStandard.Impl;

import com.cn.bean.pojo.TechnologicalStandard.GrindingProcessStandardMain;
import com.cn.bean.pojo.TechnologicalStandard.ProductRollerRoughnessSettingMain;
import com.cn.bean.pojo.TechnologicalStandard.ProductRollerRoughnessSettingSub;
import com.cn.bean.pojo.TechnologicalStandard.ReplacementCategory;
import com.cn.bean.pojo.bc.MaterList;
import com.cn.bean.pojo.bc.UseDefinition;
import com.cn.bean.pojo.sys.Sys_Sequence_Info_T;
import com.cn.config.config.Result;
import com.cn.mapper.dao.db1.bc.MaterialQualityDao;
import com.cn.mapper.dao.db1.bc.PurposesDao;
import com.cn.mapper.dao.db5.TechnologicalStandard.ProductRollerRoughnessSettingMainDao;
import com.cn.mapper.dao.db5.TechnologicalStandard.ProductRollerRoughnessSettingSubDao;
import com.cn.mapper.dao.db5.TechnologicalStandard.ReplaceProcessStandardsDao;
import com.cn.mapper.dao.db5.TechnologicalStandard.ReplacementCategoryDao;
import com.cn.mapper.dao.db5.sys.Sys_Sequence_Info_TDAO;
import com.cn.service.server.TechnologicalStandard.ProductRollerRoughnessSettingMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class ProductRollerRoughnessSettingMainServiceImpl implements ProductRollerRoughnessSettingMainService {

    private final PurposesDao purposesDao;
    //    private final Mate mate;
    private final MaterialQualityDao mate;
    private final ProductRollerRoughnessSettingMainDao replaceProcessStandardsDao;
    private final ProductRollerRoughnessSettingSubDao replacementCategoryDao;
    private final Sys_Sequence_Info_TDAO sysSequenceInfoTdao;

    @Autowired(required = false)
    public ProductRollerRoughnessSettingMainServiceImpl(Sys_Sequence_Info_TDAO sysSequenceInfoTdao, ProductRollerRoughnessSettingSubDao replacementCategoryDao, PurposesDao purposesDao, MaterialQualityDao mate, ProductRollerRoughnessSettingMainDao replaceProcessStandardsDao) {
        this.purposesDao = purposesDao;
        this.mate = mate;
        this.replaceProcessStandardsDao = replaceProcessStandardsDao;
        this.replacementCategoryDao = replacementCategoryDao;
        this.sysSequenceInfoTdao = sysSequenceInfoTdao;
    }
    @Override
    public List<ProductRollerRoughnessSettingMain> selectReplaceProcessStandards(int page, int limit, String equipmentStructure) {
        List<ProductRollerRoughnessSettingMain> grindingProcessStandardMains = replaceProcessStandardsDao.selectReplaceProcessStandards(page, limit, equipmentStructure);
        for (ProductRollerRoughnessSettingMain gr : grindingProcessStandardMains) {
            int productPurpose = gr.getProductPurpose();
            if (productPurpose != 0) {
                List<UseDefinition> useDefinitions = purposesDao.selectUseDefinitionId(gr.getProductPurpose());
                UseDefinition useDefinition = useDefinitions.get(0);
                gr.setProductPurposeName(useDefinition.getYtmc());

            }
            int material = gr.getMaterial();
            if (material != 0) {
                List<MaterList> materLists = mate.selectUseDefinitionId(gr.getMaterial());
                MaterList mater = materLists.get(0);
                gr.setMaterialName(mater.getYtmc());
            }
        }

        return grindingProcessStandardMains;
    }

    @Override
    public Result addReplaceProcessStandards(ProductRollerRoughnessSettingMain grindingProcessStandardMain, List<ProductRollerRoughnessSettingSub> replacementCategoryList) {
        Sys_Sequence_Info_T businessTypeTable = sysSequenceInfoTdao.selectSys_Sequence_Info_T("ReplaceProcessStandardsMainTable");
        int presentValue = businessTypeTable.getPresentValue();
        int stepLength = businessTypeTable.getStepLength();
        int ID = presentValue + stepLength;
        String code = String.valueOf(ID);
        if (ID > businessTypeTable.getMaxValue()) {
            return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
        }
        sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID, "ReplaceProcessStandardsMainTable");
        grindingProcessStandardMain.setId(ID);
//        grindingProcessStandardMain.setProcessCode(code);
        replaceProcessStandardsDao.addReplaceProcessStandards(grindingProcessStandardMain);
        for (ProductRollerRoughnessSettingSub replacementCategory : replacementCategoryList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("ProductRollerRoughnessSettingMainTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "ProductRollerRoughnessSettingSubTable");
            replacementCategory.setId(ID1);
            replacementCategory.setM0Id(ID);
            replacementCategoryDao.addReplacementCategory(replacementCategory);
        }
        return Result.success("200", "保存成功");
    }

    @Override
    public List<ProductRollerRoughnessSettingMain> selectReplaceProcessStandardsId(int id) {

        List<ProductRollerRoughnessSettingMain> grindingProcessStandardMains = replaceProcessStandardsDao.selectReplaceProcessStandardsId(id);
        if (!grindingProcessStandardMains.isEmpty()) {
            ProductRollerRoughnessSettingMain firstGrindingProcessStandardMain = grindingProcessStandardMains.get(0);
            int productPurpose = firstGrindingProcessStandardMain.getProductPurpose();
            if (productPurpose != 0) {
                List<UseDefinition> useDefinitions = purposesDao.selectUseDefinitionId(firstGrindingProcessStandardMain.getProductPurpose());
                UseDefinition useDefinition = useDefinitions.get(0);
                firstGrindingProcessStandardMain.setProductPurposeName(useDefinition.getYtmc());

            }
            int material = firstGrindingProcessStandardMain.getMaterial();
            if (material != 0) {
                List<MaterList> materLists = mate.selectUseDefinitionId(firstGrindingProcessStandardMain.getMaterial());
                MaterList mater = materLists.get(0);
                firstGrindingProcessStandardMain.setMaterialName(mater.getYtmc());
            }

            List<ProductRollerRoughnessSettingSub> replacementCategoryList = replacementCategoryDao.selectReplacementCategoryId(id);
            firstGrindingProcessStandardMain.setProductRollerRoughnessSettingSubList(replacementCategoryList);
        }

        return grindingProcessStandardMains;
    }

    @Override
    public Result deleteGrindingProcessStandardMain(List<ProductRollerRoughnessSettingMain> idList) {
        for (ProductRollerRoughnessSettingMain grindingProcessStandardMain : idList) {
            int id = grindingProcessStandardMain.getId();
            Integer integer = replaceProcessStandardsDao.selectShZT(id);
            if (integer > 0) {
                return Result.failure("500", "已审核的数据不能进行删除操作");
            }
        }
        for (ProductRollerRoughnessSettingMain grindingProcessStandardMain : idList) {
            int id = grindingProcessStandardMain.getId();
            replaceProcessStandardsDao.deleteGrindingProcessStandardMain(id);
            replacementCategoryDao.deleteReplacementCategory(id);

        }
        return Result.success("200", "删除成功");
    }

    @Override
    public Result updateGrindingProcessStandardMain(ProductRollerRoughnessSettingMain grindingProcessStandardMain, List<ProductRollerRoughnessSettingSub> replacementCategoryList) {
        int id = grindingProcessStandardMain.getId();
        replacementCategoryDao.deleteReplacementCategory(id);
        for (ProductRollerRoughnessSettingSub replacementCategory : replacementCategoryList) {

            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("ProductRollerRoughnessSettingMainTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "ProductRollerRoughnessSettingSubTable");
            replacementCategory.setId(ID1);
            replacementCategory.setM0Id(id);
            replacementCategoryDao.addReplacementCategory(replacementCategory);
        }
        replaceProcessStandardsDao.updateGrindingProcessStandardMain(grindingProcessStandardMain);
        return Result.success("200", "修改成功");
    }

    @Override
    public Result updateShGrindingProcessStandardMain(List<ProductRollerRoughnessSettingMain> grindingProcessStandardMainList, int state, String reviewer, String reviewerID, String reviewTime) {
        for (ProductRollerRoughnessSettingMain grindingProcessStandardMain : grindingProcessStandardMainList) {
            replaceProcessStandardsDao.updateShGrindingProcessStandardMain(grindingProcessStandardMain.getId(), state, reviewer, reviewerID, reviewTime);
        }

        if (state == 1) {
            return Result.success("200", "审核成功");

        }
        return Result.success("200", "弃审成功");

    }
}
